home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7569 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Assembler / C++ class not working!
  5. Date: 23 Feb 1996 22:07:43 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4gldrf$f29@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe15.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 23, 1996 01:55:06 in article <Assembler / C++ class not working!>,
  15. 'csmecher@unixg.ubc.ca (Alec or Graeme Smecher)' wrote: 
  16.  
  17.  
  18. >    I have defined a class and, within it, several unsigned int's and a 
  19. >few functions. The functions have imbedded assembler in them. If I 
  20. >don't use assembler, I can access these variables in the function just 
  21. >fine. If I introduce imbedded assembler, it's as if the variables 
  22. >aren't there! What is wrong? Here is a condensed version: (Sorry if 
  23. >there's any typos) 
  24. >class MyClass { 
  25. >    public: 
  26. >    unsigned int a; 
  27. >    void function(); 
  28. >    }; 
  29. >void MyClass::function () { 
  30. >    asm { 
  31. >        mov ax,a 
  32. >          ... Do stuff ... 
  33. >        } 
  34. >    } 
  35. >For some reason, this will not compile!!! 
  36. >Please help!!! 
  37.  
  38. Many compilers use a register for keeping track of the "this" pointer. 
  39. References to data members of the object then are just offsets 
  40. from the address in this register.  For example, if you code  
  41.  
  42. int foo = bar; 
  43.  
  44. where bar is a data member, the compiler generates something 
  45. like: 
  46.  
  47.    mov  eax,dword ptr[ebx+4]; 
  48.  
  49. where [ebx+4] contains a pointer to the data member.  When you 
  50. reference a data member in assembler code, the assembler is not 
  51. "smart" enough to know that bar is not a an address but a set of 
  52. instructions to the compiler.  Of course, one could argue that the 
  53. compiler should detect the situation and substitute the appropriate 
  54. reference, but the one I know about doesn't. 
  55.  
  56. Your only course of action is to declare a temporary local  
  57. variable to hold the value of the data member.  E.g., 
  58.  
  59.   int Myclass::memberfunc () 
  60.    { 
  61.       int xbar = bar; 
  62.      asm { 
  63.          mov eax,xbar 
  64.          .... 
  65.        } 
  66.    } 
  67.  
  68. This is not as expensive as it sounds.  One or two extra clock 
  69. cycles is all it costs you,depending on the machine and 
  70. compiler.  In any case, negligible. 
  71.   
  72.  
  73. -- 
  74. Pete Grant 
  75. Kalevi, Inc. 
  76. Software Engineering & development
  77.